package com.zbcz.erp.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.core.content.ContextCompat
import com.zbcz.erp.R
import com.zbcz.erp.bean.DictItem
import com.zbcz.erp.databinding.CustomSpinnerItemBinding


class CustomSpinnerAdapter(
    private val context: Context,
    private val items: Array<DictItem>
) : ArrayAdapter<DictItem>(context, 0, items) {

    override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
        val binding: CustomSpinnerItemBinding = if (convertView == null) {
            val inflater = LayoutInflater.from(context)
            val dropDownView = inflater.inflate(R.layout.custom_spinner_item, parent, false)
            CustomSpinnerItemBinding.bind(dropDownView)
        } else {
            CustomSpinnerItemBinding.bind(convertView)
        }

        if (position == 0) {
            binding.spinnerItemText.setTextColor(
                ContextCompat.getColor(
                    context,
                    R.color.text_color_hint
                )
            )
        } else {
            binding.spinnerItemText.setTextColor(ContextCompat.getColor(context, R.color.black))
        }
        val item = getItem(position)
        binding.spinnerItemText.text = item?.title
        return binding.root
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val binding: CustomSpinnerItemBinding = if (convertView == null) {
            val inflater = LayoutInflater.from(context)
            val view = inflater.inflate(R.layout.custom_spinner_item, parent, false)
            CustomSpinnerItemBinding.bind(view)
        } else {
            CustomSpinnerItemBinding.bind(convertView)
        }
        if (position == 0) {
            binding.spinnerItemText.setTextColor(
                ContextCompat.getColor(
                    context,
                    R.color.text_color_hint
                )
            )
        } else {
            binding.spinnerItemText.setTextColor(ContextCompat.getColor(context, R.color.black))
        }
        val item = getItem(position)
        binding.spinnerItemText.text = item?.title
        return binding.root
    }
}

